import { createRouter, createWebHashHistory} from "vue-router";
import UserLogin from "@/views/UserManage/UserLogin.vue";
import mainpage from "@/views/frame/mainpage.vue";
import {useUserStore} from "../store/userStore.js";

const routes = [
    {
        path: '/login',
        component:UserLogin
    },
    {
        name: "mainpage",
        path: '/',
        component:mainpage,
        children :[
            {
                name: 'customer/checkIn',
                path: '/customer/checkIn',
                component:function (){
                    return import('@/views/CustomerManage/CheckIn.vue')
                }
            },
            {
                name: 'customer/outRecords',
                path: '/customer/outRecords',
                component:function (){
                    return import('@/views/CustomerManage/OutwardRegister.vue')
                }
            },
            {
                name: 'customer/preference',
                path: '/customer/preference',
                component: function () {
                    return import('@/views/CustomerManage/MealPreference.vue')
                }
            },
            {
                name: 'customer/checkOut',
                path: '/customer/checkOut',
                component:function (){
                    return import('@/views/CustomerManage/CheckOut.vue')
                }
            },
            {
                name: 'customer/meal',
                path: '/customer/meal',
                component:function (){
                    return import('@/views/CustomerManage/Meal.vue')
                }

            } ,
            {
                name: 'customer/usermassage',
                path: '/customer/usermassage',
                component:function (){
                    return import('@/views/CustomerManage/UserMassage.vue')
                }
            },
            {
                name: 'bed/bedMap',
                path: '/bed/bedMap',
                component:function (){
                    return import('@/views/BedManage/bedMap.vue')
                }
            },
            {
                name: 'bed/bedManage',
                path: '/bed/bedManage',
                component:function (){
                    return import('@/views/BedManage/bedManage.vue')
                }
            },
            {
                name: 'nurse/nurseLevel',
                path: '/nurse/nurseLevel',
                component: function (){
                    return import('@/views/NurseManage/NurseLevel.vue')
                } // 新增路由
            },
            {
                name: 'nurse/nurseSetting',
                path: '/nurse/nurseSetting',
                component: function (){
                    return import('@/views/NurseManage/NurseSetting.vue')
                } // 新增路由
            },
            {
                name: 'nurse/nurseRecords',
                path: '/nurse/nurseRecords',
                component: function (){
                    return import('@/views/NurseManage/NurseRecords.vue')
                } // 新增路由
           },
            {
                name: 'UserRegister',
                path: '/UserRegister',
                component: function(){
                    return import('@/views/UserSetting/UserRegister.vue')
                }
            },{
                name: 'UserManage',
                path: '/UserManage',
                component: function(){
                    return import('@/views/UserSetting/UserManage.vue')
                }
            },      {
                name: 'nurse/nurseItem',
                path: '/nurse/nurseItem',
                component: function () {
                    return import('@/views/NurseManage/NurseItem.vue')
                }
            },
            // {
            //     name: 'ServiceCustomerSetting',
            //     path: '/ServiceCustomerSetting',
            //     component: function(){
            //         return import('@/views/HealthManage/ServiceCustomerSetting.vue')
            //     }
            // }

        ]
    }
]

const router = createRouter ({
    history:createWebHashHistory(),
    routes
})

const whiteList=['/login']
router.beforeEach(
    (to, from, next)=>{
        const path = to.path
        const userStore = useUserStore();

        if(whiteList.includes(path)){
            console.log("路由守卫：白名单直接跳转");
            next();
            return true;
        }

        if(localStorage.getItem('token')){
            console.log("路由守卫：通过");
            next();
            return true;
        } else {
            console.log("路由守卫：未登录");
            next("/login")
            return false;
        }
    }
)

export default router;